//
//  LineViewController.m
//  PeaPodAgent
//
//  Created by MasterLing on 2017/11/4.
//  Copyright © 2017年 MasterLing. All rights reserved.
//

#import "LineViewController.h"
#import "LineTableCell.h"
#import "CollectionModel.h"
#import "LineCollectionCell.h"





@interface LineViewController ()

@property (nonatomic, strong) NSMutableArray *cityArray;

@property (nonatomic, strong) NSMutableArray *lineArray;

@property (nonatomic, assign) NSInteger section;

@property (nonatomic, assign) NSInteger row;

@end

@implementation LineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (NSMutableArray *)cityArray{
    if (!_cityArray) {
        _cityArray = [NSMutableArray array];
    }
    return _cityArray;
}

- (NSMutableArray *)lineArray{
    if (!_lineArray) {
        _lineArray = [NSMutableArray array];
    }
    return _lineArray;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Hard coded here for demo purpose
    return self.cityArray.count;
}

//tableview delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.lineArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    CollectionModel *model = self.lineArray[indexPath.row];
    if (model.is_open) {
        //高度计算方法：cell的高度*cell的个数/2
        CGFloat height = 200;
        
        return height;
    }
    return 60;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *reuseId = @"LineTableCell";
    LineTableCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseId];
    if (!cell) {
        cell = [[LineTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseId];
    }
    [cell.collectionView reloadData];
    CollectionModel *model = self.lineArray[indexPath.row];
    if (model.is_open) {
        if (self.cityArray.count - 1 == indexPath.row) {
            cell.collectionView.frame = CGRectMake(cell.collectionView.frame.origin.x, cell.collectionView.frame.origin.y, cell.collectionView.frame.size.width, cell.frame.size.height - 60);
        }
    }
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UIView *headview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 40)];
    headview.backgroundColor = [UIColor orangeColor];
    UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, 200, 20)];
    label.text = @"这是测试的demo";
    [headview addSubview:label];
    return headview;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 60;
}

#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.section != -1 && self.row != -1) {
        [self changeModel];
        if (indexPath.section == self.section && indexPath.row == self.row) {
            self.section = -1;
            self.row = -1;
            [self.tableView reloadData];
            return;
        }
    }
    self.section = indexPath.section;
    self.row = indexPath.row;
    [self changeModel];
    [self.tableView reloadData];
    
    LineTableCell *cell = (LineTableCell *) [tableView cellForRowAtIndexPath:indexPath];
    [cell.collectionView reloadData];
    NSIndexPath *scrollIndexPath = [NSIndexPath indexPathForRow:self.row inSection:self.section];
    [self.tableView scrollToRowAtIndexPath:scrollIndexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
}

- (void) changeModel{
    CollectionModel *model = self.lineArray[self.row];
    model.is_open = !model.is_open;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.lineArray.count;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    return CGSizeMake(200, 120);
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    LineCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LineCollectionCell" forIndexPath:indexPath];
    return cell;
}
#pragma mark <UICollectionViewDelegate>
// 选中某个cell
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
}
#pragma mark  目录提示
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    //kvc
    return @[@"a",@"b"];
}

@end
